﻿package mlc.haxe.input.command;

/**
 * A command is added to a console, and can be executed from the console.
 * 
 * Commands are used internally, and can be ignored in common cases. However, sometimes it is useful to store the command in a
 * variable. Doing so allows you to destroy the command easily.
 */
class Command {
	/**
	 * A variable only used in the destroy method.
	 */
	private var key:String;
	/**
	 * A variable only used in the destroy method.
	 */
	private var remove:String -> Bool;
	/**
	 * Creates a new command. The passed remove method and key are used in the destroy method.
	 */
	public function new(remove:String -> Bool, key:String):Void {
		// Store the remove method and the key.
		this.remove = remove;
		this.key = key;
	}
	/**
	 * Destroys the command. The console that created this command will no longer execute this command. Commands cannot be
	 * "undestroyed".
	 */
	public inline function destroy():Void {
		remove(key);
		// There is a bug with inlining. The line above returns a boolean value, and if it wasn't for the line below, that value
		// would leak and be returned by this method too.
		null;
	}
	#if debug
	private function toString():String {
		return "[Command]";
	}
	#end
}